using System;
using UnityEngine;

[CreateAssetMenu(fileName = "CityBuildingElementData", menuName = "Voodoo/CityBuildingElementData", order = 1)]
public class CityBuildingElementData : ScriptableObject
{
	public Action OnUnlock;

	public int UnlockLevel;

	public int UnlockPrice;

	public string ID;

	public bool GiveGems;

	public float GainPerMinute;

	public const string LAST_COLLECT = "VoodooCBCollect";

	private DateTime _lastCollect;

	public const string IS_UNLOCKED = "VoodooCBUnlocked";

	private bool _unlocked;

	public const string CURRENT_LEVEL = "VoodooCBLevel";

	private int _level;

	public bool Unlocked
	{
		get
		{
			return _unlocked;
		}
		set
		{
			_unlockedPref = (_unlocked = value);
		}
	}

	public DateTime LastCollect
	{
		get
		{
			return _lastCollect;
		}
		set
		{
			_lastCollectPref = (_lastCollect = value);
		}
	}

	public int MoneyToCollect
	{
		get
		{
			double num = (DateTime.Now - LastCollect).TotalMinutes;
			DateTime lastX2CollectPref = CityBuildingManager.Instance.LastX2CollectPref;
			TimeSpan timeSpan = lastX2CollectPref - LastCollect;
			if (timeSpan.TotalMinutes > 0.0)
			{
				num += (double)(3f * Mathf.Min(5f, (int)(DateTime.Now - lastX2CollectPref).TotalMinutes));
			}
			else if (timeSpan.TotalMinutes > -5.0)
			{
				num += (double)(3 * Mathf.Min((int)timeSpan.TotalMinutes, (int)(DateTime.Now - lastX2CollectPref).TotalMinutes));
			}
			if (num >= 60.0)
			{
				num = 60.0;
			}
			else if (num <= 5.0)
			{
				num = 0.0;
			}
			return (int)((double)((float)GainPerHours / 60f) * num);
		}
	}

	public int Level
	{
		get
		{
			return _level;
		}
		set
		{
			_levelPref = (_level = value);
		}
	}

	private DateTime _lastCollectPref
	{
		get
		{
			string @string = NintendoPlayerPrefs.GetString("VoodooCBCollect" + ID, "");
			if (string.IsNullOrEmpty(@string))
			{
				_lastCollectPref = DateTime.Now.AddHours(-1.0);
				return _lastCollectPref;
			}
			return DateTime.FromOADate(double.Parse(@string));
		}
		set
		{
			NintendoPlayerPrefs.SetString("VoodooCBCollect" + ID, value.ToOADate().ToString());
		}
	}

	private bool _unlockedPref
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("VoodooCBUnlocked" + ID, 0) == 1;
		}
		set
		{
			if (value)
			{
				OnUnlock?.Invoke();
			}
			NintendoPlayerPrefs.SetInt("VoodooCBUnlocked" + ID, value ? 1 : 0);
		}
	}

	private int _levelPref
	{
		get
		{
			return NintendoPlayerPrefs.GetInt("VoodooCBLevel" + ID, 0);
		}
		set
		{
			NintendoPlayerPrefs.SetInt("VoodooCBLevel" + ID, value);
		}
	}

	public int GainPerHours => (int)(GainPerMinute * 60f * (float)(Level + 1));

	public string GainPerHoursString => GainPerHours + (GiveGems ? " gem" : "$") + " /h";

	public int NextLevelGainPerHours => (int)(GainPerMinute * 60f * (float)(Level + 2));

	public string NextLevelGainPerHoursString => NextLevelGainPerHours + (GiveGems ? " gem" : "$") + " /h";

	public void Initialize()
	{
		_lastCollect = _lastCollectPref;
		_unlocked = _unlockedPref;
		_level = _levelPref;
	}

	public bool IsRewardAvailable()
	{
		_ = _lastCollect;
		DateTime dateTime = _lastCollect.AddMinutes(3.0);
		return DateTime.Now >= dateTime;
	}

	public bool ShowNotification()
	{
		_ = _lastCollect;
		DateTime dateTime = _lastCollect.AddMinutes(30.0);
		return DateTime.Now >= dateTime;
	}

	public int UpgradePrice()
	{
		return UnlockPrice * ((Level + 1) * (Level + 1) + 1);
	}
}
